package com.xiaoxu.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.rental.entity.Order;
import com.xiaoxu.rental.service.OrderService;
import com.xiaoxu.rental.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaoxu
 * @since 2024-07-24
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/rental/order")
@SuppressWarnings("all")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Value("${auto.info.maintain-mileage}")
    private Integer maintainMileage;
    @PostMapping
    public Result save(@RequestBody Order order){
        return orderService.insert(order)?Result.success():Result.fail();
    }


    @PostMapping("/unfinished/{start}/{size}")
    public Result searchUnfinished(@PathVariable int start,
                                   @PathVariable int size,
                                   @RequestBody Order order){
    Page<Order> page = new Page<>(start,size);
    return Result.success(orderService.searchUnfinished(page,order));
    }



    @ApiOperation(value = "更新应保次数")
    @PutMapping
    public Result update(@RequestBody Order order){
        return orderService.returnAuto(order,maintainMileage)?Result.success():Result.fail();
    }

    @ApiOperation(value = "订单详细信息")
    @PostMapping("/{start}/{size}")
    public Result search(@PathVariable int start,@PathVariable int
            size,@RequestBody Order order){
        Page<Order> page = new Page<>(start,size);
        return Result.success(orderService.search(page,order));
    }

    @ApiOperation(value = "统计是否有违章")
    @GetMapping("/countViolation/{autoId}")

    public Result countViolation(@PathVariable Integer autoId){
        long count=orderService.countViolation(autoId);
        return Result.success(count);
    }
    @GetMapping("/doReturnDeposit/{orderId}")
    public Result doReturnDeposit(@PathVariable Integer orderId){
        Order order = orderService.getById(orderId);
        order.setDepositReturn(1);
        return orderService.updateById(order)?
                Result.success():Result.fail();
    }


}
